package com.snowRemovingUser.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.snowRemovingUser.domain.TInterfaces;
import com.snowRemovingUser.dto.WeatherDetail;
import com.snowRemovingUser.dto.WeatherDetailModel;
import com.snowRemovingUser.dto.WeatherDto;
import com.snowRemovingUser.dto.WeatherEvent;
import com.snowRemovingUser.mapper.TInterfacesMapper;
import com.snowRemovingUser.service.ITInterfacesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据管理Service业务层处理
 *
 * @author liguang
 * @date 2023-08-21
 */
@Service
public class TInterfacesServiceImpl implements ITInterfacesService
{
    @Autowired
    private TInterfacesMapper tInterfacesMapper;

    /**
     * 查询数据管理
     *
     * @param id 数据管理主键
     * @return 数据管理
     */
    @Override
    public TInterfaces selectTInterfacesById(Long id)
    {
        return tInterfacesMapper.selectTInterfacesById(id);
    }

    /**
     * 查询数据管理列表
     *
     * @param tInterfaces 数据管理
     * @return 数据管理
     */
    @Override
    public List<TInterfaces> selectTInterfacesList(TInterfaces tInterfaces)
    {
        return tInterfacesMapper.selectTInterfacesList(tInterfaces);
    }

    /**
     * 新增数据管理
     *
     * @param tInterfaces 数据管理
     * @return 结果
     */
    @Override
    public int insertTInterfaces(TInterfaces tInterfaces)
    {
        return tInterfacesMapper.insertTInterfaces(tInterfaces);
    }

    /**
     * 修改数据管理
     *
     * @param tInterfaces 数据管理
     * @return 结果
     */
    @Override
    public int updateTInterfaces(TInterfaces tInterfaces)
    {
        return tInterfacesMapper.updateTInterfaces(tInterfaces);
    }

    /**
     * 批量删除数据管理
     *
     * @param ids 需要删除的数据管理主键
     * @return 结果
     */
    @Override
    public int deleteTInterfacesByIds(Long[] ids)
    {
        return tInterfacesMapper.deleteTInterfacesByIds(ids);
    }

    /**
     * 删除数据管理信息
     *
     * @param id 数据管理主键
     * @return 结果
     */
    @Override
    public int deleteTInterfacesById(Long id)
    {
        return tInterfacesMapper.deleteTInterfacesById(id);
    }


    /**
     * 天气数据插入
     * @param weatherDto
     * @throws JsonProcessingException
     */
    @Override
    public void AddWeatherDetails(WeatherDto weatherDto) throws JsonProcessingException {

        TInterfaces tInterfacesToParent = new TInterfaces();
        tInterfacesToParent.setCustomer(weatherDto.getCustomer());
        Integer status = weatherDto.getStatus();

        tInterfacesToParent.setPushType(weatherDto.getPushType());
        List<TInterfaces> TInterfacesList = new ArrayList<>();

        //推送成功
        if (status==0){

            tInterfacesToParent.setStatus(status);
            tInterfacesToParent.setPushInfo(weatherDto.getPushInfo());
            tInterfacesToParent.setPushTime(new Date());
            tInterfacesToParent.setParentId(0L);
            tInterfacesMapper.insertTInterfaces(tInterfacesToParent);
            List<WeatherDetailModel> weatherList = weatherDto.getWeatherDetails();
            for (WeatherDetailModel weatherDetailModel : weatherList) {

                TInterfaces tInterfaces = new TInterfaces();
                tInterfaces.setPushRoad(weatherDetailModel.getMonitorCode());
                ObjectMapper objectMapper = new ObjectMapper();
                String pushInfo = objectMapper.writeValueAsString(weatherDetailModel.getWeatherDetails());
                tInterfaces.setPushInfo(pushInfo);
                tInterfaces.setParentId(tInterfacesToParent.getId());
                tInterfaces.setPushType(tInterfacesToParent.getPushType());
                TInterfacesList.add(tInterfaces);
            }
            tInterfacesMapper.addTInterfacesList(TInterfacesList);
        //推送失败
        }else {

            tInterfacesToParent.setStatus(status);
            tInterfacesToParent.setPushInfo(weatherDto.getPushInfo());
            tInterfacesToParent.setPushTime(new Date());
            tInterfacesToParent.setParentId(0L);
            tInterfacesMapper.insertTInterfaces(tInterfacesToParent);
        }
    }


    /**
     * 天气数据详情
     * @param id
     * @return
     * @throws JsonProcessingException
     */
    @Override
    public List<WeatherDetail> selectExpandDetail(Long id) throws JsonProcessingException {

        TInterfaces tInterfaces = tInterfacesMapper.selectTInterfacesById(id);

        String pushInfo = tInterfaces.getPushInfo();

        ObjectMapper objectMapper = new ObjectMapper();
        List<WeatherDetail> weatherList = objectMapper.readValue(pushInfo, new TypeReference<List<WeatherDetail>>(){});



        return weatherList;
    }


    /**
     * 天气事件插入
     * @param weatherDto
     * @throws JsonProcessingException
     */
    @Override
    public void AddEventDetails(WeatherDto weatherDto) throws JsonProcessingException {


        TInterfaces tInterfacesToParent = new TInterfaces();
        tInterfacesToParent.setCustomer(weatherDto.getCustomer());
        Integer status = weatherDto.getStatus();

        tInterfacesToParent.setPushType(weatherDto.getPushType());
        List<TInterfaces> TInterfacesList = new ArrayList<>();

        //推送成功
        if (status==0){

            tInterfacesToParent.setStatus(status);
            tInterfacesToParent.setPushInfo(weatherDto.getPushInfo());
            tInterfacesToParent.setPushTime(new Date());
            tInterfacesToParent.setParentId(0L);
            tInterfacesMapper.insertTInterfaces(tInterfacesToParent);
            List<WeatherEvent> weatherEventDtoList = weatherDto.getWeatherEventDtoList();
            for (WeatherEvent weatherEvent : weatherEventDtoList) {

                TInterfaces tInterfaces = new TInterfaces();
                tInterfaces.setPushRoad(weatherEvent.getMonitorCode());
                ObjectMapper objectMapper = new ObjectMapper();
                String pushInfo = objectMapper.writeValueAsString(weatherEvent);
                tInterfaces.setPushInfo(pushInfo);
                tInterfaces.setParentId(tInterfacesToParent.getId());
                tInterfaces.setPushType(tInterfacesToParent.getPushType());
                TInterfacesList.add(tInterfaces);
            }
            tInterfacesMapper.addTInterfacesList(TInterfacesList);

        }else {

            tInterfacesToParent.setStatus(status);
            tInterfacesToParent.setPushInfo(weatherDto.getPushInfo());
            tInterfacesToParent.setPushTime(new Date());
            tInterfacesToParent.setParentId(0L);
            tInterfacesMapper.insertTInterfaces(tInterfacesToParent);
        }
    }


    /**
     * 天气事件展开详情
     * @param id
     * @return
     * @throws JsonProcessingException
     */
    @Override
    public List<WeatherEvent> selectEventExpandDetail(Long id){


        List<WeatherEvent> weatherEventList = new ArrayList<>();

        TInterfaces tInterfaces = tInterfacesMapper.selectTInterfacesById(id);

        String pushInfo = tInterfaces.getPushInfo();
        WeatherEvent weatherEventVo = JSON.parseObject(pushInfo, WeatherEvent.class);
        weatherEventList.add(weatherEventVo);
        return weatherEventList;
    }
}
